--[[
此文件为热更已存在的服务
执行方法:
1. 连接console：telnet 127.0.0.1 9666
2. 查看需要热更的服务地址：list
:00000004	snlua cdummy
:00000006	snlua datacenterd
:00000007	snlua service_mgr
:00000009	snlua clusterd
:0000000a	snlua gate
:0000000b	snlua login login 1
:0000000c	snlua login login 2
:0000000d	snlua agent_mgr agent_mgr 0
:0000000e	snlua scene_mgr scene_mgr 0
:0000000f	snlua scene scene 1001
:00000010	snlua scene scene 1002
:00000011	snlua admin admin 0
:00000012	snlua mysql mysql 0
:00000013	snlua register register 0
:00000014	snlua debug_console 127.0.0.1 9666
:00000015	snlua gateway gateway 1
:00000016	snlua gateway gateway 2
:00000017	snlua agent agent 7

3. 执行：inject :00000017 fix/hotfix.lua
4. 若后续产生的新服务也需要更新，需要修改原文件，再控制台输入clearcache
5. 优化：启动一个hotfix服务，将需要热更的服务注册到hotfix服务下，在控制台使用call命令，对hotfix发送消息，批量更新所有服务：call :00000017 "client",4
--]]


local handles = _P.lua.M.client_handle[4]
local print = _G.print

-- 获取upvalue
local function get_up(f)
    local u = {}
    if not f then
        return u
    end
    local i = 1
    while true do
        local name = debug.getupvalue(f, i)
        if name == nil then
            return u
        end
        u[name] = i
        i = i + 1
        print("uv:" .. name .. " i:" .. i)
    end
    return u
end

-- 将upvalue关联到函数
local function uv_join(f, old_f, old_uv)
    local i = 1
    while true do
        local name = debug.getupvalue(f, i)
        if not name then
            break
        end

        if old_uv[name] then
            debug.upvaluejoin(f, i, old_f, old_uv[name])
        end
        i = i + 1
    end
end

local old_handle = handles
local up = get_up(old_handle)

local skynet, s, table_utils, game_cmd
handles = function(gate, msg)
    print("hotfix success")
    skynet.error("hotfix success")
    local uid = msg.uid
    if s.player:get_uid() ~= uid then
        skynet.error("此接口不允许查看其他人的信息")
        return
    end

    local player = s.player

    local player_base_info = player:get_player_base_info()
    skynet.error("player base info:" .. table_utils.table2string(player_base_info))

    local response = {
        error_code = game_cmd.ERROR_CODE.SUCCESS,
        player_base_info = player_base_info
    }

    skynet.send(gate, "lua", "send", uid, game_cmd.SERVER_CMD.PLAYER_BASE_INFO, response)
end

uv_join(handles, old_handle, up)
_P.lua.M.client_handle[4] = handles